package com.youhong.teethcare.utils;

import android.content.Context;

import com.youhong.cuptime.blesdk.*;
import com.youhong.teethcare.*;
import com.youhong.teethcare.R;
import com.youhong.teethcare.javabeans.BrushingRecord;

import java.util.Calendar;

import static com.youhong.teethcare.utils.DeviceCmd.Cmd_GetBrushHistoryRecord;
import static com.youhong.teethcare.utils.DeviceCmd.Cmd_GetBrushTime;
import static com.youhong.teethcare.utils.DeviceCmd.Cmd_GetDeviceVersion;

public class BraceletDevice extends Bledevice {

    public BraceletDevice(Context context, String address) {
        super(context, address);

        // TODO Auto-generated constructor stub
    }

    public BraceletDevice(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public void connected(String address) {

        deviceMac = address;
        if (bleService != null) {
            bleService.initBluetoothDevice(deviceMac, context);
        } else {
            initServiceIntent();
        }
    }

    public void sendData(byte[] value) {

        if (shujuCharateristic == null || !isConnected())
            return;
        shujuCharateristic.setValue(value);
        this.writeValue(shujuCharateristic);
    }

    public void syncTime(Calendar calendar) {

        byte[] cmd = new byte[16];
        cmd[0] = 0x1;
        cmd[1] = Util.ConvertDecimal2BCD((byte) (calendar.get(Calendar.YEAR) - 2000));
        cmd[2] = Util.ConvertDecimal2BCD((byte) (calendar.get(Calendar.MONTH) + 1));
        cmd[3] = Util.ConvertDecimal2BCD((byte) (calendar.get(Calendar.DAY_OF_MONTH)));
        cmd[4] = Util.ConvertDecimal2BCD((byte) (calendar.get(Calendar.HOUR_OF_DAY)));
        cmd[5] = Util.ConvertDecimal2BCD((byte) (calendar.get(Calendar.MINUTE)));
        cmd[6] = Util.ConvertDecimal2BCD((byte) (calendar.get(Calendar.SECOND)));
        cmd[15] = crc16(cmd);
        sendData(cmd);

    }

    public void getTime() {

        byte[] cmd = new byte[16];
        cmd[0] = 0x41;
        cmd[15] = crc16(cmd);
        sendData(cmd);

    }

    public void setBrushingTime(int time) {

        byte[] commands = new byte[16];
        commands[0] = 0x6;

        switch (time) {
            case 90:
                commands[1] = 20;
                commands[2] = 25;
                commands[3] = 25;
                commands[4] = 20;
                break;
            case 150:
                commands[1] = 35;
                commands[2] = 40;
                commands[3] = 40;
                commands[4] = 35;
                break;
            case 180:

                commands[1] = 40;
                commands[2] = 50;
                commands[3] = 50;
                commands[4] = 40;

                break;

            default:
                commands[1] = 30;
                commands[2] = 30;
                commands[3] = 30;
                commands[4] = 30;
                break;
        }

        crc16(commands);
        commands[15] = crc16(commands);
        sendData(commands);
    }

    public void setBrushingTimeType(int type) {

        byte[] commands = new byte[16];
        commands[0] = 0x6;

        switch (type) {
            case 0:
                commands[1] = 20;
                commands[2] = 25;
                commands[3] = 25;
                commands[4] = 20;
                break;
            case 1:
                commands[1] = 30;
                commands[2] = 30;
                commands[3] = 30;
                commands[4] = 30;
                break;
            case 2:
                commands[1] = 35;
                commands[2] = 40;
                commands[3] = 40;
                commands[4] = 35;
                break;
            case 3:

                commands[1] = 40;
                commands[2] = 50;
                commands[3] = 50;
                commands[4] = 40;

                break;


        }

        crc16(commands);
        commands[15] = crc16(commands);
        sendData(commands);
    }

    public void getBrushingTime() {

        byte[] commands = new byte[16];
        commands[0] = Cmd_GetBrushTime;
        commands[15] = crc16(commands);
        sendData(commands);

    }

    public void setBrushingTime(int mode, int percent) {
        byte[] commands = new byte[16];
        commands[0] = 0x55;
        commands[1] = (byte) mode;
        commands[2] = (byte) percent;
        commands[15] = crc16(commands);
        sendData(commands);

    }

    public void getBrushingData(byte val) {
        byte[] commands = new byte[16];

        commands[0] = Cmd_GetBrushHistoryRecord;
        commands[1] = val;
        commands[15] = crc16(commands);
        sendData(commands);
    }

    public void getVersion() {
        byte[] commands = new byte[16];

        commands[0] = Cmd_GetDeviceVersion;
        commands[15] = crc16(commands);
        sendData(commands);
    }

    public void mcuReset() {
        byte[] commands = new byte[16];

        commands[0] = 0x2E;
        commands[15] = crc16(commands);
        sendData(commands);
    }

    public void setHand(byte hand) {
        byte[] commands = new byte[16];
        commands[0] = DeviceCmd.Cmd_SetHand;
        commands[1] = hand;
        commands[15] = crc16(commands);
        sendData(commands);
    }

    public void getHand() {
        byte[] commands = new byte[16];
        commands[0] = DeviceCmd.Cmd_GetHand;
        commands[15] = crc16(commands);
        sendData(commands);
    }


    public void upgradeFirmware() {
        byte[] commands = new byte[16];
        commands[0] = 0x47;
        commands[15] = crc16(commands);
        sendData(commands);
    }

    public void factoryReset() {
        byte[] commands = new byte[16];

        commands[0] = 0x12;
        commands[15] = crc16(commands);
        sendData(commands);
    }

    /**
     * 开始刷牙，时间过短没有保存数据，切换界面会在onDesotry发送停止刷牙指令
     * 数据保存好后会发送停止刷牙指令
     * 刷完16个区域会先发送停止刷牙指令
     *
     * @param cmd
     * @param area
     */
    public void guideBrushing(byte cmd, byte area) {

        byte[] commands = new byte[16];
        commands[0] = DeviceCmd.Cmd_GuideBrushing;
        commands[1] = cmd;
        commands[2] = area;
        commands[15] = crc16(commands);
        sendData(commands);
    }

    //查询刷牙状态
    public void getBrushingStatus() {
        byte[] commands = new byte[16];
        commands[0] = 0x21;
        commands[15] = crc16(commands);
        sendData(commands);
    }

    public void setBrushingStatus(int mode) {
        byte[] commands = new byte[16];
        commands[0] = 0x21;
        commands[1] = 1;
        commands[2] = (byte) mode;
        commands[15] = crc16(commands);
        sendData(commands);
    }

    public void setBrushingDiy(int mode) {
        byte[] commands = new byte[16];
        commands[0] = 0x21;
        commands[1] = 2;
        commands[2] = (byte) mode;
        commands[15] = crc16(commands);
        sendData(commands);
    }

    public void getBattery() {

        byte[] commands = new byte[16];
        commands[0] = 0x13;
        commands[15] = crc16(commands);
        sendData(commands);
    }

    public byte crc16(byte[] values) {

        byte val = 0;
        for (int i = 0; i < 15; i++) {
            val += values[i];
        }
        return val;
    }

    public byte crc20(byte[] values) {

        byte val = 0;
        for (int i = 0; i < 19; i++) {
            val += values[i];
        }
        return val;
    }

    public void setGear(byte gear) {

        byte[] commands = new byte[16];
        commands[0] = 0x21;
        commands[1] = 0x1;
        commands[2] = gear;
        commands[15] = crc16(commands);
        sendData(commands);

    }

    public void saveBrushRecord(BrushingRecord record) {
        byte[] commands = new byte[35];
        commands[0] = DeviceCmd.Cmd_SaveData;
        int year = record.getStartTime().get(Calendar.YEAR) - 2000;
        int month = record.getStartTime().get(Calendar.MONTH) + 1;
        int day = record.getStartTime().get(Calendar.DAY_OF_MONTH);
        int hour = record.getStartTime().get(Calendar.HOUR_OF_DAY);
        int minute = record.getStartTime().get(Calendar.MINUTE);
        int second = record.getStartTime().get(Calendar.SECOND);
        commands[1] = Util.ConvertDecimal2BCD((byte) year);
        commands[2] = Util.ConvertDecimal2BCD((byte) month);
        commands[3] = Util.ConvertDecimal2BCD((byte) day);
        commands[4] = (byte) (Util.ConvertDecimal2BCD((byte) hour) + 48);
        commands[5] = Util.ConvertDecimal2BCD((byte) minute);
        commands[6] = Util.ConvertDecimal2BCD((byte) second);
        commands[7] = (byte) (record.getTotalTime() & 0xFF);
        commands[8] = (byte) ((record.getTotalTime() >> 0x8) & 0xFF);
        commands[9] = (byte) record.getBrushMode();
        commands[10] = (byte) record.getBrushLevel();
        //下门牙外
        if ((record.getArea1() & 0x1) != 0) {
            commands[19] = 10;
        }
        //下门牙内
        if ((record.getArea1() & 0x4) != 0) {
            commands[20] = 10;
        }
        //下门牙外
        if ((record.getArea2() & 0x1) != 0) {
            commands[21] = 10;
        }
        //下门牙内
        if ((record.getArea2() & 0x4) != 0) {
            commands[22] = 10;
        }
        //左下外
        if ((record.getArea3() & 0x1) != 0) {
            commands[23] = 10;
        }
        //左下中
        if ((record.getArea3() & 0x2) != 0) {
            commands[24] = 10;
        }
        //左下内
        if ((record.getArea3() & 0x4) != 0) {
            commands[25] = 10;
        }
        //左上外
        if ((record.getArea4() & 0x1) != 0) {
            commands[26] = 10;
        }
        //左上中
        if ((record.getArea4() & 0x2) != 0) {
            commands[27] = 10;
        }

        //左上内
        if ((record.getArea4() & 0x4) != 0) {
            commands[28] = 10;
        }

        //右下外
        if ((record.getArea5() & 0x1) != 0) {

            commands[29] = 10;
        }

        //右下中
        if ((record.getArea5() & 0x2) != 0) {
            commands[30] = 10;
        }

        //右下内
        if ((record.getArea5() & 0x4) != 0) {
            commands[31] = 10;
        }

        //右上外
        if ((record.getArea6() & 0x1) != 0) {

            commands[32] = 10;
        }

        //右上中
        if ((record.getArea6() & 0x2) != 0) {
            commands[33] = 10;
        }

        //右上内
        if ((record.getArea6() & 0x4) != 0) {
            commands[34] = 10;
        }

        //表示是指导刷牙数据， 是APP 主动存储的
        sendData(commands);
    }
}
